// by Xeno

private ["_ok","_display","_waypoints","_marker_pos","_can_call","_air_taxis"];
if (!X_Client) exitWith {};

disableserialization;
_can_call = WithAirTaxi;

// Controlling if this player has already called an air taxi
_air_taxis = ["airtaxi_available",[]] call XNetGetJIP;
if (isNil "_air_taxis") then {_air_taxis=[]};
if (typeName _air_taxis != "ARRAY") then {_air_taxis=[]};

diag_log format["Busy air taxis: %1", _air_taxis];
if (_player in _air_taxis) then {_can_call = false;};
if (!_can_call) exitWith {"An air taxi is already in the air!" call XfHQChat};

if ((FLAG_BASE distance player < 500) and (!WithAirTaxiWaypoints)) then {
	If (!WithAirTaxiWaypoints) then {"You are less than 500 m away from the base, no airtaxi for you!" call XfHQChat; _can_call = False;};
};

diag_log format["WithWaypoints %1",WithAirTaxiWaypoints];

if (_can_call) then {
	if (dialog) then {closeDialog 0};

	X_AIRTAXI_DESTINATION = "";
	X_AIRTAXI_WAYPOINTS = [];

	If (WithAirTaxiWaypoints) then {
		_ok = createDialog "XD_AirTaxiDialog";
		// _display = uiNameSpace getVariable "D_TAXI_DIALOG";
		for "_mt" from 0 to 19 do {
			_m_marker_name = format ["m_airtaxi_waypoint_%1", _mt];
			_marker = createMarkerLocal [_m_marker_name, [0,0,0]];
			_m_marker_name setMarkerAlphaLocal 0;
			_m_marker_name setMarkerShapeLocal "ICON";
			_m_marker_name setMarkerTypeLocal "hd_dot";
			_m_marker_name setMarkerTextLocal Format["%1",_mt];
		};
		// With each single click we add a marker which is added to the waypoints list.
		//onMapSingleClick "If (count X_AIRTAXI_WAYPOINTS < 20) then { _markerName = Format['m_airtaxi_waypoint_%1', count X_AIRTAXI_WAYPOINTS]; diag_log Format['WP: %1 at %2',_markerName, _pos]; _markerName setMarkerPosLocal [_pos select 0, _pos select 1, 0]; _markerName setMarkerAlphaLocal 1; X_AIRTAXI_WAYPOINTS set [count X_AIRTAXI_WAYPOINTS, _markerName]; _markerName setMarkerTypeLocal 'hd_end'; if (count X_AIRTAXI_WAYPOINTS > 1) then { _prev_marker = Format['m_airtaxi_waypoint_%1',(count X_AIRTAXI_WAYPOINTS) - 2]; _prev_marker setMarkerLocalType 'hd_dot';}; }; true;";
		onMapSingleClick "If (count X_AIRTAXI_WAYPOINTS < 20) then { _markerName = Format['m_airtaxi_waypoint_%1', count X_AIRTAXI_WAYPOINTS]; diag_log Format['WP: %1 at %2',_markerName, _pos]; _markerName setMarkerPosLocal [_pos select 0, _pos select 1, 0]; _markerName setMarkerAlphaLocal 1; X_AIRTAXI_WAYPOINTS set [count X_AIRTAXI_WAYPOINTS, _markerName]; _markerName setMarkerTypeLocal 'hd_end'; if (count X_AIRTAXI_WAYPOINTS > 1) then { _prev_marker = Format['m_airtaxi_waypoint_%1',(count X_AIRTAXI_WAYPOINTS) - 2]; _prev_marker setMarkerTypeLocal 'hd_dot';}; }; true;";
		waitUntil {X_AIRTAXI_DESTINATION != "" || !dialog || !alive player};
		onMapSingleClick "";
		diag_log Format["Closed dialog: %1 (%2)", X_AIRTAXI_DESTINATION, count X_AIRTAXI_WAYPOINTS];
		if (!alive player) then {
			closedialog 0;
			X_AIRTAXI_DESTINATION = "";
			X_AIRTAXI_WAYPOINTS = [];
		} else {
			_waypoints = [];
			If (count X_AIRTAXI_WAYPOINTS > 0) then {
				diag_log "Adding Waypoints";
				{
					_marker_pos = getMarkerPos _x;
					diag_log Format[" -- %1", _marker_pos];
					_waypoints set [count _waypoints, [_marker_pos select 0, _marker_pos select 1, 0]];
				} foreach X_AIRTAXI_WAYPOINTS;
				X_AIRTAXI_WAYPOINTS = _waypoints;
			};
		};
		for "_mt" from 0 to 19 do {
			_m_marker_name = format ["m_airtaxi_waypoint_%1", _mt];
			deleteMarkerLocal _m_marker_name;
		};
	} else {
		X_AIRTAXI_DESTINATION = "BASE";
	};

	If (X_AIRTAXI_DESTINATION != "") then {
		If (X_AIRTAXI_DESTINATION == "BASE" OR (count X_AIRTAXI_WAYPOINTS == 0)) then {
			if (FLAG_BASE distance player < 500) then {
				"You are less than 500 m away from the base, no air taxi for you!" call XfHQChat;
			} else {
				[player, "Calling in air taxi..."] call XfSideChat;
				diag_log "Calling taxi to base";
				["d_air_taxi2", player] call XNetCallEvent;
			};
		} else {
			[player, "Calling in air taxi..."] call XfSideChat;
			diag_log "Calling taxi to waypoints";
			["d_air_taxi2", [player,X_AIRTAXI_WAYPOINTS]] call XNetCallEvent;
		};
	} else {
		"Air taxi canceled!" call XfHQChat;
	};
};

